#!/usr/bin/python3
# -*- coding: utf-8 -*
"""
Description: File Listing parser.
Class: ListParser
"""
import re

from ..libs.log import LOGGER
from ..parsers import Parser


class ListParser(Parser):
    """
    class ListParser is the parser that parses the file list to the formmated data.
    """

    def parse_content(self, key, content):
        """
        Parse the list file data to formatted data.

        Args:
            key (str): the main key of the content.
            content (str): raw data.

        Returns:
            dict: parsed content.
            bool: parse succeed or fail.
        """
        LOGGER.debug(self.regex_config)
        res = {}
        re_obj = re.compile(
            r"(?P<type>\S)(?P<permission>\S+) (?P<links>\d+) (?P<owner>\w+) +"
            r"(?P<group>\w+) +(?P<size>\d+) (?P<month>\w+) +(?P<day>\d+) +"
            r"(?P<time>\S+) +(?P<name>\S+)(?:\s+->\s+)?(?P<link>\S+)?")
        for line in content:
            matched = re_obj.match(line)
            if matched is not None:
                item = matched.groupdict()
                if item['link']:
                    res[item['name']] = item['link']

        if len(res) == 0:
            LOGGER.warning("no data in %s", key)

        return res, True


def get_class():
    """
    get ListParser class.

    Args:

    Returns:
        class
    """
    return ListParser
